function decoderBlocks = WiSiL_Decoder_blockSeparation( bitsRx, encoder, ...
                                                        blocks )
% WISIL_DECODER_BLOCKSEPARATION Separate the code blocks at the receiver.
%
%  DECODERBLOCKS = WiSiL_Decoder_blockSeparation( BITSRX, ENCODER, BLOCKS )              
%  separates the received bit strean in BITSRX into codeblocks, according
%  to the encoder parameters in ENCODER and the blocks configuration in
%  BLOCKS. Each code block is returned as an element of the cell array
%  DECODERBLOCKS


% ???????
%   created


numBlocks = blocks.numBlocks;
C1 = blocks.C1;
C2 = blocks.C2;
K1 = blocks.K1;
K2 = blocks.K2;
B = 1/encoder.R;

tailbits = 0;

fillerBits = blocks.fillerBits;
if(strcmpi(encoder.FEC,'CTC')==1)
    tailbits = encoder.TurboCoding.tailbits;
end

decoderBlocks = zeros([],B);

for k = 1: numBlocks
    if k <=C2
        if k ==1
            decoderBlocks{k,1} = reshape( bitsRx( 1: ( K2 + tailbits - ....
                                                      fillerBits ) / ...
                                                     encoder.R ), [], B)';
            bitsRx( 1 : ( K2 + tailbits - fillerBits ) / encoder.R ) = [];
        else
            decoderBlocks{k,1} = reshape(bitsRx(1:(K2+tailbits)/encoder.R),[],B)';
            bitsRx(1:(K2+tailbits)/encoder.R) =[];
        end
    else
        decoderBlocks{k,1} = reshape(bitsRx(1:(K1+tailbits)/encoder.R),[],B)';
        bitsRx(1:(K1+tailbits)/encoder.R) =[];
    end
end
decoderBlocks{1,1}=[NaN(1/encoder.R,fillerBits) decoderBlocks{1,1}(:,:)];